package com.pinc.exam;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

public class Test_Start extends FlipActivity {
	private Question_Packet mQP;
	private TextView[] mSelButton;
	private static final int OVERVIEW_REQUEST = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.test_flip);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

		if ( savedInstanceState == null )
			mQP = (Question_Packet) getIntent().getSerializableExtra("packet");
		else
			mQP = (Question_Packet) savedInstanceState.getSerializable("saved_packet");

		cur_qstn = mQP.cur_qstn;
		initWidget();
		showQuestion();
	}

	@Override
	protected void onPause() {
		super.onPause();
		save2File();
	}

	@Override
	protected void initWidget() {
		super.initWidget();

		/* set upbar */
		tv_title.setTextSize(16);

		b_prev.setVisibility(View.VISIBLE);
		b_prev.setText(getResources().getString(R.string.str_exit));
		b_prev.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});

		b_next.setVisibility(View.VISIBLE);
		b_next.setText(getResources().getString(R.string.str_overview));
		b_next.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(Test_Start.this, Test_Overview.class);
				intent.putExtra("packet", mQP);
				startActivityForResult(intent, OVERVIEW_REQUEST);
			}
		});

		mSelButton = new TextView[GParam.SELECT_MAX+1];
		mSelButton[0] = (TextView) findViewById(R.id.textViewA);
		mSelButton[1] = (TextView) findViewById(R.id.textViewB);
		mSelButton[2] = (TextView) findViewById(R.id.textViewC);
		mSelButton[3] = (TextView) findViewById(R.id.textViewD);
		mSelButton[4] = (TextView) findViewById(R.id.textViewE);
		mSelButton[5] = (TextView) findViewById(R.id.textViewO);

		mSelButton[5].setOnLongClickListener(slctLongListener);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if ( requestCode == OVERVIEW_REQUEST )
		{
			switch (resultCode)
			{
			case Activity.RESULT_OK:
				cur_qstn = data.getIntExtra("number", cur_qstn);
				flipper.setInAnimation(null);
				flipper.setOutAnimation(null);
				flipper.setDisplayedChild(0);
				showQuestion();
				break;
			case Activity.RESULT_FIRST_USER:
				finish();
				break;
			default:
				break;
			}
		}
	}

	/* private Listener */
	private View.OnLongClickListener slctLongListener = new View.OnLongClickListener()
	{
		@Override
		public boolean onLongClick(View v) {
			if ( cur_qstn < mQP.mQstn_total-1 )
				showNext();
			return true;
		}
	};

	/* private Listener */
	private class slctTouchListener implements View.OnTouchListener
	{
		private int pos;
		private TextView[] tv_sel;

		public slctTouchListener(int pos, TextView[] tv_sel) {
			this.pos = pos;
			this.tv_sel = tv_sel;
		}

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			if (event.getAction() == MotionEvent.ACTION_DOWN)
			{
				mQP.mArr_ansPaper[cur_qstn] = pos;
				markSelection(tv_sel, cur_qstn);
			}
			return false;
		}
	}

	@Override
	protected String getQuestion(int qstn_no) {
		return mQP.mQuestion[qstn_no];
	}

	@Override
	protected String[] getSelections(int qstn_no) {
		return mQP.mArr_sel[qstn_no];
	}

	@Override
	protected void markSelection(TextView[] tv_sel, int qstn_no) {
		for( int i=0; i< GParam.SELECT_MAX; i++)
		{
			if ( i == mQP.mArr_ansPaper[qstn_no]-1)
			{
				tv_sel[i].setTextColor(getResources().getColor(R.color.selected));
			}
			else
			{
				tv_sel[i].setTextColor(Color.BLACK);
			}
		}
	}

	@Override
	protected int getQuestionCnt() {
		return mQP.mQstn_total;
	}

	@Override
	protected void postHandle(int no) {
		for ( int i = 0; i< GParam.SELECT_MAX; i++)
		{
			if ( i < mQP.mArr_sel[no].length)
			{
				mSelButton[i].setVisibility(View.VISIBLE);
				mSelButton[i].setOnTouchListener(new slctTouchListener(i+1,getCurPageTextViews()));
				mSelButton[i].setOnLongClickListener(slctLongListener);
			}
			else
			{
				mSelButton[i].setVisibility(View.GONE);
			}
		}
		mSelButton[5].setOnTouchListener(new slctTouchListener(0,getCurPageTextViews()));

		tv_title.setText(String.format("%s\n(%d/%d)", getResources().getString(R.string.str_quiz), no+1, mQP.mQstn_total));
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		mQP.cur_qstn = cur_qstn;
		outState.putSerializable("saved_packet", mQP);
	}

	private void save2File()
	{
		try {
			FileOutputStream fos = openFileOutput(GParam.QUIZ_FILENAME, Context.MODE_PRIVATE);
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			mQP.cur_qstn = cur_qstn;
			oos.writeObject(mQP);
			oos.close();
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	protected int getImageIndex(int qstn_no) {
		return mQP.mImgID[qstn_no];
	}

}
